
<template>
  <main-container headerText="代理管理 >> 设置代理推荐人">
    <div class="content"  :key="showPageKey">
      <top-option>
        <div slot="left-box">
          <input-search
            lableName="手机号"
            inputValue="phone"
            :tThis="this"
            inputPlaceholder="请输入手机号"
          />

          <input-search
            lableName="用户昵称"
            inputValue="nickName"
            :tThis="this"
            inputPlaceholder="请输入用户昵称"
          />

          <select-search
            lableName="级别"
            selectIdName="readeIntegral"
            :tThis="this"
            selectPlaceholder="请选择级别"
            :selectOpts="gradeOpts"
            selectWidth="150px;"
            labelWidth="80px;"
          />
        </div>
        <div slot="right-box">
          <div class="sub">
            <el-button @click="handleSearch" class="orange">查询</el-button>
          </div>
        </div>
      </top-option>

      <top-option>
        <div slot="left-box">
          <el-button @click="add_agent_recF" class="orange">设置为代理推荐人</el-button>
        </div>
        <div slot="right-box">
          <div class="sub"></div>
        </div>
      </top-option>

      <!-- 列表栏 -->
      <div class="list">
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%; border-radius: 4px"
          class="tableBox"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column label="序号" width="undefined">
            <template slot-scope="scope">
              {{ scope.row.index || "-" }}
            </template>
          </el-table-column>
          <el-table-column label="用户昵称" width="undefined">
            <template slot-scope="scope">
              {{ scope.row.nickName || "-" }}
            </template>
          </el-table-column>
          <el-table-column label="手机号" width="undefined">
            <template slot-scope="scope">
              {{ scope.row.phone || "-" }}
            </template>
          </el-table-column>
          <el-table-column label="会员级别" width="undefined">
            <template slot-scope="scope">
              {{ scope.row.grade || "-" }}
            </template>
          </el-table-column>
          <el-table-column label="创客级别" width="undefined">
            <template slot-scope="scope">
              {{ scope.row.guestName || "-" }}
            </template>
          </el-table-column>
          <el-table-column label="最近登录时间" width="undefined">
            <template slot-scope="scope">
              {{ scope.row.createDate || "-" }}
            </template>
          </el-table-column>

          <el-table-column label="操作" width="0">
            <template slot-scope="scope"> </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="paging">
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPageIdx"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="onePageSize"
            layout="total, prev, pager, next, jumper"
            :total="pageCount"
          ></el-pagination>
        </div>

        <div class="confirm-btn">
          <el-button class="paging-btn" @click="handleEnter" size="small"
            >确定</el-button
          >
        </div>
      </div>
    </div>
  </main-container>
</template>

  <script>
import jhTools from "@/utils/jhTools"; //工具函数
import API from "@/utils/api_config.js"; //引入api

import main_container from "@/components/main_container";
import top_option from "@/components/subCon/topOption";
import input_search from "@/components/subCon/inputSearch";
import select_search from "@/components/subCon/selectSearch";
import date_search from "@/components/subCon/dateSearch";

var _this;

export default {
  components: {
    "main-container": main_container,
    "top-option": top_option,
    "input-search": input_search,
    "select-search": select_search,
    "date-search": date_search,
  },
  props: {},
  data() {
    return {
      //列表
      tableData: [],
      multipleSelection: [],
      //分页
      currentPageIdx: 1, //当前页与跳转框的初始值
      pageCount: 1, //总条数
      onePageSize: 10, //每页的条数

      formData: {
        phone: null,
        nickName: null,
        readeIntegral: null,
      },

      
      showPageKey: 100,

      gradeOpts: [
        { value: "1", label: "普粉" },
        { value: "2", label: "铁粉" },
        { value: "3", label: "超粉" },
        { value: "4", label: "V1" },
        { value: "5", label: "V2" },
        { value: "6", label: "V3" },
        { value: "7", label: "V4" },
        { value: "8", label: "V5" },
        { value: "9", label: "V6" },
        { value: "10", label: "V7" },
        { value: "", label: "全部" },
      ],

      allMethods: "",
    };
  },
  mounted() {
    _this = this;
    this.allMethods = this.getAllMethods();
       //页面数据重置 1
    if (this.recoverSearchF) {
      this.recoverSearchF({ _this: this });
    }

    this.initRequest();

    this.getInitData();
  },
  methods: {
    /* 设置为代理推荐人 */
    add_agent_recF() {
      var tips = "", //提示
        idsArr = []; //id数组
      var reqUrl = this.api.agent_m.set_agent_recom;

      if(!this.$route.query.recId) {
        this.tools.Model({
          msg: "缺少记录id信息!"
        });
        return;
      }

      if (this.multipleSelection.length == 0) {
        this.tools.Model({
          msg: "请选择推荐人!",
        });
        return;
      } else if(this.multipleSelection.length != 1) {
        this.tools.Model({
          msg: "只能设置一个推荐人！",
        });
        return;
      } else {
        // this.multipleSelection.forEach((item, index) => {
        //   idsArr.push(item.id);
        // });
        // var idsStr = idsArr.toString();
        //请求
        this.tools.basePost({
          url: reqUrl,
          params: {
            id: this.$route.query.recId,
            recommendUser: this.multipleSelection[0].id, 
          },
          success: (res) => {
            if (res.code == 200) {
              this.tools.Model({
                msg: "设置成功！",
                success() {
                  //清除所有选中状态
                    _this.toggleSelection();
                    _this.getInitData();
                    _this.$router.push({
                      path: "/agent_m_agent_verify_table",
                    });
                },
              });
            }
          },
          fail: (err) => {
          },
        });
      }
    },

    //查询
    handleSearch() {
      this.allMethods.handleSearch();
    },

    /* 方法抽离集合 */
    getAllMethods() {
      var _this = this;
      return {
        //查询
        handleSearch() {
          var { phone, nickName, readeIntegral } = _this.formData;
           //保存搜索条件
          _this.currentPageIdx = 1;
          //保存数据 2
          if (_this.saveSearchF) {
            _this.saveSearchF({ _this: _this });
          }
          _this.LingChuang_request.handleSearch({
            phone,
            nickName,
            readeIntegral,
            pageIndex: _this.currentPageIdx,
            pageSize: _this.onePageSize,
          });
        },
      };
    },

    //表格默认函数
    //初始化
    initRequest() {
      this.LingChuang_request = jhTools.LingChuang_request({
        this: this,
        pageUrl: API.node_m.page,
        searchUrl: API.node_m.page,
        searchData: {},
      });
      // console.log(this.LingChuang_request);
    },
    //加载数据
    getInitData: function () {
      var _this = this;
      this.LingChuang_request.getInitData(
        {
          pageIndex: this.currentPageIdx,
          pageSize: this.onePageSize,
          nodeState: 0, //不是节点
        },
        function (res) {
          //初始格式化数据
        }
      );
    },
    //格式化数据
    formatData() {
      this.LingChuang_request.formatData();
    },
    //格式化页面
    formatPaging(res) {
      this.LingChuang_request.formatPaging(res);
    },
    //页数跳转
    handleSizeChange(val) {
      this.LingChuang_request.handleSizeChange(val);
      //分页数据 3
      if (this.saveSearchF) {
        this.saveSearchF({ _this: this });
      }
    },
    //页面点击跳转
    handleCurrentChange(val) {
      this.LingChuang_request.handleCurrentChange(val);
      //分页数据 3
      if (this.saveSearchF) {
        this.saveSearchF({ _this: this });
      }
    },
    //enter跳转
    handleEnter() {
      this.LingChuang_request.getPagingData(
        this.currentPageIdx,
        this.onePageSize
      );
      //分页数据 3
      if (this.saveSearchF) {
        this.saveSearchF({ _this: this });
      }
    },
    //分页数据
    getPagingData(pageIndex, pageSize) {
      this.LingChuang_request.handleEnter(pageIndex, pageSize);
    },
    //列表勾选1
    toggleSelection(rows) {
      this.LingChuang_request.toggleSelection(rows);
    },
    //列表勾选2
    handleSelectionChange(val) {
      this.LingChuang_request.handleSelectionChange(val);
    },
    //表格默认函数

    /* 图片预览 */
    previewImgF({ imgUrl }) {
      this.tools.touchGEvent({
        eName: "previewImg",
        imgUrl,
      });
    },

    
    /* 恢复搜索状态 6 */
    recoverSearchF({ _this }) {
      _this.tools.recoverSearchF({
        _this,
        subItemName: "/agent_m_set_agent_rec_t",
      });
    },

    /* 保存搜索状态 7 */
    saveSearchF({ _this }) {
      _this.tools.saveSearchF({
        _this,
        subItemName: "/agent_m_set_agent_rec_t",
      });
    },
    
  },
};
</script>

<style lang="scss" scoped>
// @import '~styles/mixins.scss';
// @import '~styles/variables.scss';

//隐藏样式类
.hidden {
  display: none !important;
}
</style>
